/**
 * Created by Administrator on 2018/2/7.
 */
const EVENTS = {};
module.exports = {
  addEvent(event, callback) {
    if (!EVENTS.hasOwnProperty(event)) {
      EVENTS[event] = [];
    }
    let key = callback.bind(this);
    EVENTS[event].push(key);
    return key;
  },
  triggerEvent(event, param) {
    if (EVENTS.hasOwnProperty(event)) {
      for (let f of EVENTS[event]) {
        f(param);
      }
    }
  },
  delEvent(event, callback) {
    if (EVENTS.hasOwnProperty(event)) {
      let list = EVENTS[event],
        len = list.length,
        f = 0,
        n;
      if (!callback) {
        delete EVENTS[event];
      } else {
        for (; f < len; f++) {
          if (callback === list[f]) {
            n = f;
            break;
          }
        }
        if (n !== void 0) {
          list.splice(n, 1);
        }
        if (!list.length) {
          delete EVENTS[event];
        }
      }
    }
  }
};